<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="keywords">
          <el-input v-model="queryParams.keywords" placeholder="请输入关键字" clearable style="width: 200px" />
          <el-button type="primary" style="margin-left: 0; border-radius: 0" @click="handleQuery"> 搜索 </el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-card shadow="never">
      <el-table ref="tableRef" v-loading="loading" :data="tableList" highlight-current-row border>
        <el-table-column label="当前状态" prop="hasLocked" min-width="80" align="center">
          <template #default="scope">
            <el-text v-if="scope.row.hasLocked" type="danger">上锁</el-text>
            <el-text v-else type="primary">解锁</el-text>
          </template>
        </el-table-column>
        <el-table-column label="编号" prop="code" min-width="180" align="center" />
        <el-table-column label="项目名称" prop="projectName" min-width="180" align="center" />
        <el-table-column label="锁定月份" prop="lockMonth" min-width="120" align="center" />
        <el-table-column label="锁定原因" prop="lockReason" min-width="180" align="center" />
        <el-table-column label="锁定时间" prop="lockTime" min-width="100" align="center" />
        <el-table-column label="解锁时间" prop="unlockTime" min-width="160" align="center" />
        <el-table-column label="解锁意见" prop="unlockReason" min-width="160" align="center" />
        <el-table-column label="是否推送一体化平台" align="center" prop="hasPush" min-width="150">
          <template #default="scope">
            <el-text v-if="scope.row.hasPush" type="success">已推送</el-text>
            <el-text v-else type="warning">未推送</el-text>
          </template>
        </el-table-column>
        <el-table-column label="推送时间" prop="pushTime" min-width="160" align="center" />
        <el-table-column label="操作" align="center" min-width="100" fixed="right">
          <template #default="scope">
            <el-button
              v-if="permissions.push && scope.row.hasPush == false"
              type="primary"
              size="small"
              @click="handlePush(scope.row)"
            >
              推送
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { QueryProjectLockLog, PushProjectLockLog } from "@/api/admin/locklog/index";
import { ProjectLockLogQuery } from "@/api/admin/locklog/types";
import { downloadFile } from "@/utils";
import { ElTable } from "element-plus";

const permissions = ref({
  push: false,
});

const currentSystem = ref(localStorage.getItem("usertypeName"));
const isLumpContractor = ref(localStorage.getItem("isLumpContractor"));

if (currentSystem.value == "Corp" || currentSystem.value == "Manager") {
  permissions.value.push = true;
}
const total = ref(0);
const loading = ref(false);
const queryFormRef = ref(ElForm);
const tableRef = ref(ElTable);
const tableList = ref<any[]>();
const queryParams = reactive({
  pageNum: 1,
  pageSize: 20,
  keywords: "",
  type: 2, //工程担保公司
});

const handlePush = (row: any) => {
  PushProjectLockLog(row.code).then((res) => {
    if (res.success) {
      ElMessage.success("推送成功");
    }
  });
};

/* 列表查询 */
const handleQuery = async () => {
  loading.value = true;
  QueryProjectLockLog(queryParams)
    .then((res) => {
      tableList.value = res.data.list;
      queryParams.pageNum = res.data.pageNum;
      queryParams.pageSize = res.data.pageSize;
      total.value = res.data.total;
    })
    .finally(() => {
      loading.value = false;
    });
};

onMounted(() => {
  handleQuery();
});
</script>

<style lang="less" scoped>
:deep(.detail .el-dialog__body) {
  height: var(--mobileAddFormHeight);
  overflow-y: auto;
}
</style>
